package com.songlanyun.modules.pointtobalace.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.platformgivepoint.service.PlatformGivePointRecordService;
import com.songlanyun.modules.pointgiveaway.service.PointGiveAwayService;
import com.songlanyun.modules.pointtobalace.dao.BalanceToPointRecordDao;
import com.songlanyun.modules.pointtobalace.dao.PointToBalanceRecordDao;
import com.songlanyun.modules.pointtobalace.entity.BalanceToPointRecordEntity;
import com.songlanyun.modules.pointtobalace.entity.BalanceToPointStatistics;
import com.songlanyun.modules.pointtobalace.entity.PointToBalanceRecordEntity;
import com.songlanyun.modules.pointtobalace.entity.PointToBalanceStatistics;
import com.songlanyun.modules.pointtobalace.service.BalanceToPointRecordService;
import com.songlanyun.modules.pointtobalace.service.PointToBalanceRecordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


/**
 * @author zhangqh
 */
@Service("balanceToPointRecordService")
public class BalanceToPointRecordServiceImpl extends ServiceImpl<BalanceToPointRecordDao, BalanceToPointRecordEntity> implements BalanceToPointRecordService {

    @Override
    public BalanceToPointRecordEntity create(BalanceToPointRecordEntity entity) {
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public BalanceToPointRecordEntity modify(BalanceToPointRecordEntity entity) {
        return null;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<BalanceToPointRecordEntity> wrapper = new LambdaQueryWrapper<>();
        String realName = MapUtil.getStr(params, "realName");
        String mobile = MapUtil.getStr(params, "mobile");

        Date startTime = MapUtil.getDate(params, "start_time");
        Date endTime = MapUtil.getDate(params, "end_time");

        wrapper.like(StringUtils.isNotBlank(realName),BalanceToPointRecordEntity::getRealName,realName);
        wrapper.like(StringUtils.isNotBlank(mobile),BalanceToPointRecordEntity::getMobile,mobile);

        wrapper.ge(ObjectUtil.isNotNull(startTime),BalanceToPointRecordEntity::getCreateTime,startTime);
        if(ObjectUtil.isNotNull(endTime)) {
            endTime = DateUtils.addDateDays(endTime,1);
            wrapper.le(ObjectUtil.isNotNull(endTime), BalanceToPointRecordEntity::getCreateTime, endTime);
        }


        wrapper.orderByDesc(BalanceToPointRecordEntity::getCreateTime);
        IPage<BalanceToPointRecordEntity> page = this.page(
                new Query<BalanceToPointRecordEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public BalanceToPointStatistics getPointToBalanceStatistics(Map<String, Object> params) {
        return this.baseMapper.statistics(params);
    }

}
